package com.neutech.solar.util;

import com.neutech.solar.constant.Constant;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 *  图片工具类
 *  使用I/O流加载图片
 *  I:InoutStream
 *  O:Out
 */
public class ImageUtil  {

    public static Map<String,Image> maps = new HashMap<>();
    static {
        maps.put("bg",ImageUtil.getImage("bg.jpg"));
        maps.put("sun",ImageUtil.getImage("Sun.png"));
        maps.put("Mercury",ImageUtil.getImage("Mercury.png"));
        maps.put("Venus",ImageUtil.getImage("Venus.png"));
        maps.put("Earth",ImageUtil.getImage("Earth.png"));
        maps.put("Mars",ImageUtil.getImage("Mars.png"));
        maps.put("Jupiter",ImageUtil.getImage("Jupiter.png"));
        maps.put("Saturn",ImageUtil.getImage("Saturn.png"));
        maps.put("Uranus",ImageUtil.getImage("Uranus.png"));
        maps.put("Neptune",ImageUtil.getImage("海王星.png"));

        maps.put("smallPlanet",ImageUtil.getImage("冥王星.png"));

    }
    private ImageUtil(){}

    public static Image getImage(String imageName)  {

        // 加载资源路径
        URL u = ImageUtil.class.getClassLoader().getResource(Constant.IMAGE_PATH_PRE +imageName);
        // 提高稳健读取效率
        BufferedImage img = null;
        try {
            img =  ImageIO.read(u);
        }catch (Exception e){
            // 在控制台打印错误信息
            e.printStackTrace();
        }

        return img;
    }
}
